import {reqGoodsInfo, reqAddOrUpdateShopCart} from '@/api';
import {getUUID} from '@/utils/uuid_token';
//详情模块的小仓库
const state = {
  goodInfo: {},
  uuid_token: getUUID()
};
const mutations = {
  GOODINFO(state, goodInfo) {
    state.goodInfo = goodInfo
  }
};
const actions = {
  //获取产品信息的action
  async getGoodInfo({commit}, skuId) {
    let result = await reqGoodsInfo(skuId)
    if (result.code === 0) {
      commit("GOODINFO", result.data)
    }
  },
  //将商品添加到购物车中(新增或修改)
  async addOrUpdateShopCart({commit}, {skuId, skuNum}) {
    //加入购物车返回的结构
    let result = await reqAddOrUpdateShopCart(skuId, skuNum)
    if (result.code === 200) {
      return "ok"
    } else {
      return Promise.reject(new Error("fail"))
    }
  }
};
//计算属性，为了简化数据而生
const getters = {
  //面包屑导航
  categoryView(state) {
    return state.goodInfo.categoryView || {}
  },
  //sku基础信息
  skuInfo(state) {
    return state.goodInfo.info || {}
  },
  //spu销售属性
  spuSaleAttrList() {
    return state.goodInfo.saleAttrs || []
  },
  //sku图片
  skuImages() {
    return state.goodInfo.images || []
  },
  //详情图片
  desc() {
    return state.goodInfo.desc || {}
  },
  //秒杀信息
  seckillInfo() {
    return state.goodInfo.seckillInfoTo || {}
  }
};
export default {
  state,
  mutations,
  actions,
  getters
}
